package com.yanshibo.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.yanshibo.bean.Dormitory;
import com.yanshibo.bean.Floor;
import com.yanshibo.bean.QueryVo;
import com.yanshibo.bean.User;
import com.yanshibo.service.DormitoryService;

@Controller
public class DormitoryController {

	@Autowired
	private DormitoryService dormitoryService;
	
	
	/**  
	 * @Title: getDormitoryList
	 * @Description: TODO(管理员列表)
	 * @param pageNum
	 * @param model
	 * @param vo
	 * @return
	 * @author 闫诗博
	 * @date 2021-06-09 07:29:41 
	 */ 
	@RequestMapping("/list")
	public String getDormitoryList(@RequestParam(defaultValue="1")Integer pageNum,Model model,QueryVo vo) {
		PageInfo<Dormitory> pg = dormitoryService.getDormitoryList(pageNum,vo);
		List<Floor> fools = dormitoryService.getFool();
		for (Floor floor : fools) {
			System.out.println(floor);
		}
		model.addAttribute("pg", pg);
		model.addAttribute("vo", vo);
		model.addAttribute("fools", fools);
		return "dormitory_list";
	}
	
	@RequestMapping("/toTiao")
	public String toTiao(Model model,Dormitory dormitory) {
		//查询所有学生
		List<User> users = dormitoryService.getUserList();
		//查询宿舍楼
		List<Floor> fools = dormitoryService.getFool();
		//查询宿舍表
		List<Dormitory> dormitorys = dormitoryService.erji(0);
		
		
		model.addAttribute("dormitory", dormitory);
		model.addAttribute("users", users);
		model.addAttribute("fools", fools);
		model.addAttribute("dormitorys", dormitorys);
		
		return "dormitory_tiao";
	}
	
	@RequestMapping("/erji")
	@ResponseBody
	public List<Dormitory> erji(Integer fid) {
		
		return dormitoryService.erji(fid);
	}
	
	@RequestMapping("/sanji")
	@ResponseBody
	public Dormitory sanji(Integer did) {
		
		return dormitoryService.sanji(did);
	}
	
	@GetMapping("login")
	public String toLogin() {
		return "login";
	}
	
	@PostMapping("login")
	public String login(User user,HttpSession session,Model model){
		User user1= dormitoryService.login(user);
	//	
		
		if(user1!=null) {
			session.setAttribute("LOGIN_USER", user1);
			if(0==user1.getLev()) {
				return "redirect:list1";
			}
			if(1==user1.getLev()) {
				return "redirect:list";
			}
		}
		return "login";
	}
	@RequestMapping("list1")
	public String list1(Model model,HttpServletRequest request) {
		User user = (User) request.getSession().getAttribute("LOGIN_USER");
		
		PageInfo<User> info= dormitoryService.selects();
		
		Integer uid = user.getUid();
		Integer lev = user.getLev();
		
		PageInfo<Dormitory> listDor =dormitoryService.listDor();
		model.addAttribute("listDor", listDor);
		List<Dormitory> list = listDor.getList();
		
		for (Dormitory dormitory : list) {
			if(dormitory.getOutTime()==null) {
				model.addAttribute("num", dormitory.getDname());
			}
			
		}
		//存到作用域
		model.addAttribute("info", info);
		
		return "list1";
	}
}
